<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/26
 * Time: 21:37
 */

namespace app\api\http\exceptions;


/**
 * Trait ResponseJson
 * @package app\common\lib
 */
trait ResponseJson
{
    /**
     * @param array $data
     * @param int $httpCode
     * @return \think\response\Json
     */
    public function jsonSuccessResponse($data = [], $httpCode = 200)
    {
        $status = ApiErr::SUCCESS[0];
        $message = ApiErr::SUCCESS[1];
        return $this->jsonResponse($status, $message, $data, $httpCode);
    }

    /**
     * @param $status
     * @param $message
     * @param array $data
     * @param int $httpCode
     * @return \think\response\Json
     */
    public function jsonErrResponse($status, $message, $data = [], $httpCode = 200)
    {
        $status = $httpCode == 404 ? ApiErr::API_REQUEST_ERROR[0] : $status;
        $message = $httpCode == 404 ? ApiErr::API_REQUEST_ERROR[1] . ':'.$message : $message;
        return $this->jsonResponse($status, $message, $data, $httpCode == 404 ? 404 : 200);
    }

    /**
     * @param $status
     * @param $message
     * @param $data
     * @param int $httpCode
     * @return \think\response\Json
     */
    public function jsonResponse($status, $message, $data, $httpCode = 200)
    {
        $result = [
            'code' => $status, // 业务状态码
            'message' => $message,
            'data' => $data
        ];
        return json($result, $httpCode);
    }

}